/* 
 * Copyright (C) 2015 Federico Migliavacca, Leonardo Orsello
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package Boundary;

import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author fede
 */
@Named(value = "loginGUI")
@RequestScoped
public class LoginGUI {

    private String username;
    private String password;

    /**
     * This method give the username of user
     *
     * @return username as String
     */
    public String getUsername() {
        return username;
    }

    /**
     * This method set the username of user
     *
     * @param username as string
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * This method give the password of user
     *
     * @return password as string
     */
    public String getPassword() {
        return password;
    }

    /**
     * This method set the username of user
     *
     * @param password as string
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * This method perform login. Next redirect to /registereduser folder. Only
     * logged user could access to this directory thanks to realm.
     *
     * @return String
     */
    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        try {
            request.login(this.username, this.password);
            return "/registereduser/calendar";
        } catch (ServletException e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Login Failed."));
            return null;
        }

    }

    /**
     * This method perform logout. Next redirect to login page.
     *
     * @return String
     */
    public String logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        request.getSession().invalidate();
        return "/login?faces-redirect=true";

    }

    /**
     * Creates a new instance of LoginGUI
     */
    public LoginGUI() {
    }

}
